package guiTest;

import java.awt.Component;
import java.util.EventObject;

import javax.swing.AbstractCellEditor;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.TableCellEditor;

public class SpinnerCellEditor extends AbstractCellEditor implements TableCellEditor, DocumentListener
{
	private static final long serialVersionUID = 1L;
	Integer currentValue;
	JTextField spinner;
	
	
	public SpinnerCellEditor()
	{
		spinner = new JTextField(3);	
		//spinner.getDocument().addDocumentListener(this);
	}
	
	@Override
	public Object getCellEditorValue() 
	{
		return spinner.getText().replaceAll("[a-zA-Z]", "");
	}

	@Override
    public Component getTableCellEditorComponent(JTable table, Object value,
    	boolean isSelected, int row, int column)
	{
		String val = value.toString().replaceAll("[a-zA-Z]", "");
		spinner.setText(val);
		return spinner;
	}
	

	public boolean isCellEditable(EventObject evt)
	{
		return true;
	}

	@Override
	public void changedUpdate(DocumentEvent arg0) 
	{
		// TODO Auto-generated method stub
		replaceStuff();
		
	}

	@Override
	public void insertUpdate(DocumentEvent arg0) 
	{
		// TODO Auto-generated method stub
		replaceStuff();
	}

	@Override
	public void removeUpdate(DocumentEvent arg0) 
	{
		// TODO Auto-generated method stub
		replaceStuff();
		
	}
	
	public void replaceStuff()
	{
		spinner.setText(spinner.getText().replaceAll("[a-zA-Z]", ""));
	}
	

}
